home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11241 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: 22 Mar 1996 11:24:41 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4iuuppINN3s2@keats.ugrad.cs.ubc.ca>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi> <4iumf8$604@madeline.INS.CWRU.Edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4iumf8$604@madeline.INS.CWRU.Edu>,
  12. Michael A. Balfour <mab22@po.CWRU.Edu> wrote:
  13.  >
  14.  >In a previous article, aril@cmt.lpr.mail.carel.fi (Ari Lukumies) says:
  15.  >
  16.  >>How about something like:
  17.  >>
  18.  >>    struct {
  19.  >>        int    flag;
  20.  >>        union {
  21.  >>            char    text[80];
  22.  >>            int    num;
  23.  >>            double    fpoint;
  24.  >>        } data;
  25.  >>    } test;
  26.  >>
  27.  >
  28.  >Wasn't the original goal of the variant records to avoid chewing up
  29.  >extra memory?  As I understand it, union uses as much memory as the
  30.  >largest member.  In this case, even if you are only storing an int, you
  31.  >still use 80 bytes, right?
  32.  
  33. I don't think that you are right on this one, but I would have to do a little
  34. digging to be sure. In Pascal, it will also be true that the variant record
  35. will take up the same space as the largest member. This has to be the case,
  36. simply, because the type field is dynamic, not static. What would happen if you
  37. made an array of variant records, and then decided to switch their type? 
  38.  
  39. If you want to avoid ``chewing memory'', a language with ``is-a'' inheritance,
  40. such as Oberon, will do the trick. You would declare a few members as a base
  41. record, and then extend it in various ways into derived objects of various
  42. lengths.
  43.  
  44. I will check up on the Pascal semantics of variant records when next I have the
  45. opportunity.
  46. -- 
  47.  
  48.